package com.videoweb.domain.agent.valueobject;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;

/**
 * 模型配置值对象
 * 封装AI模型的各种参数配置
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ModelConfig {
    
    private String modelName;
    private BigDecimal temperature;
    private Integer maxTokens;
    private BigDecimal topP;
    private Integer topK;
    private BigDecimal frequencyPenalty;
    private BigDecimal presencePenalty;
    
    /**
     * 创建默认配置
     */
    public static ModelConfig createDefault() {
        return ModelConfig.builder()
                .modelName("deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")
                .temperature(new BigDecimal("0.7"))
                .maxTokens(2000)
                .topP(new BigDecimal("1.0"))
                .topK(50)
                .frequencyPenalty(new BigDecimal("0.0"))
                .presencePenalty(new BigDecimal("0.0"))
                .build();
    }
    
    /**
     * 创建代码生成优化配置
     */
    public static ModelConfig createForCodeGeneration() {
        return ModelConfig.builder()
                .modelName("deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")
                .temperature(new BigDecimal("0.3")) // 代码生成使用较低温度
                .maxTokens(4000) // 代码生成需要更多tokens
                .topP(new BigDecimal("0.95"))
                .topK(40)
                .frequencyPenalty(new BigDecimal("0.1"))
                .presencePenalty(new BigDecimal("0.1"))
                .build();
    }
    
    /**
     * 创建对话优化配置
     */
    public static ModelConfig createForConversation() {
        return ModelConfig.builder()
                .modelName("deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")
                .temperature(new BigDecimal("0.8")) // 对话使用较高温度增加创造性
                .maxTokens(2000)
                .topP(new BigDecimal("1.0"))
                .topK(50)
                .frequencyPenalty(new BigDecimal("0.2"))
                .presencePenalty(new BigDecimal("0.2"))
                .build();
    }
    
    /**
     * 验证配置有效性
     */
    public boolean isValid() {
        return modelName != null && !modelName.trim().isEmpty()
            && temperature != null && temperature.compareTo(BigDecimal.ZERO) >= 0 && temperature.compareTo(new BigDecimal("2.0")) <= 0
            && maxTokens != null && maxTokens > 0 && maxTokens <= 32000
            && topP != null && topP.compareTo(BigDecimal.ZERO) >= 0 && topP.compareTo(BigDecimal.ONE) <= 0;
    }
    
    /**
     * 获取温度参数的double值
     */
    public double getTemperatureAsDouble() {
        return temperature != null ? temperature.doubleValue() : 0.7;
    }
    
    /**
     * 获取topP参数的double值
     */
    public double getTopPAsDouble() {
        return topP != null ? topP.doubleValue() : 1.0;
    }
    
    /**
     * 复制配置并修改温度
     */
    public ModelConfig withTemperature(BigDecimal newTemperature) {
        return ModelConfig.builder()
                .modelName(this.modelName)
                .temperature(newTemperature)
                .maxTokens(this.maxTokens)
                .topP(this.topP)
                .topK(this.topK)
                .frequencyPenalty(this.frequencyPenalty)
                .presencePenalty(this.presencePenalty)
                .build();
    }
    
    /**
     * 复制配置并修改最大tokens
     */
    public ModelConfig withMaxTokens(Integer newMaxTokens) {
        return ModelConfig.builder()
                .modelName(this.modelName)
                .temperature(this.temperature)
                .maxTokens(newMaxTokens)
                .topP(this.topP)
                .topK(this.topK)
                .frequencyPenalty(this.frequencyPenalty)
                .presencePenalty(this.presencePenalty)
                .build();
    }
}